/**
 *Copyright(c) HNA SYSTEMS Co., LTD
 *
 *@date 2013-7-23
 * 	
 *Original Author: 郑兴(zhengxing)
 *
 *ChangeLog
 *
 */
package com.hnas.sys.dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Component;

import com.hnas.core.dao.BaseDaoImpl;
import com.hnas.sys.orm.MenuPackage;

/**
 * @author 郑兴(zhengxing)
 *
 */
@Component("menuPackageDAO")
public class MenuPackageDAOImpl extends BaseDaoImpl<MenuPackage> implements MenuPackageDAO {
	public MenuPackageDAOImpl() {
		super(MenuPackage.class);
	}

	/*
	 * (non-Javadoc)
	 * @see com.hna.dt.dao.system.MenuPackageDAO#loadAll()
	 */
	@SuppressWarnings("unchecked")
	public List<MenuPackage> loadAll() {
		Query query = entityManager.createQuery("From MenuPackage where statusStr='SHOW' order by priority");
		return query.getResultList();
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.hna.dt.dao.system.MenuPackageDAO#search(java.lang.String)
	 */
	public List<MenuPackage> search(String packageName) {
		Query query = entityManager.createQuery("From MenuPackage where name like ? and statusStr='SHOW' order by priority");
		query.setParameter(1, "%" + packageName + "%");
		return query.getResultList();
	}
	public List<MenuPackage> loadPackageById(Long[] ids) {
		// TODO Auto-generated method stub
		if (ids.length==0)
			return new ArrayList<MenuPackage>();
		StringBuffer sb = new StringBuffer(" from MenuPackage t where t.statusStr='SHOW' and  t.id in (");
		Object[] objs = new Object[ids.length];
		for (int i=0;i<ids.length;i++){
			sb.append("?").append(i+1).append(",");
			objs[i] = ids[i];
		}
		sb.deleteCharAt(sb.length()-1);
		sb.append(") order by t.priority");
		List<MenuPackage > ls= this.findByQuery(sb.toString(), objs);
		return ls;
	}

	public MenuPackage getPackgeByName(String name) {
		// TODO Auto-generated method stub

		Query query = entityManager
				.createQuery("From MenuPackage where name = ? and statusStr='SHOW' order by id desc ");
		query.setParameter(1, "" + name.trim() + "");
		List<MenuPackage> ls = query.getResultList();
		if (ls.size() > 0)
			return ls.get(0);
		return null;
	}
}
